package Eyjafjalla.action;

import Eyjafjalla.patch.CloudPatch;
import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.utility.NewQueueCardAction;
import com.megacrit.cardcrawl.actions.utility.UnlimboAction;
import com.megacrit.cardcrawl.actions.utility.WaitAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.core.AbstractCreature;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;

import java.util.ArrayList;

public class DiscardAndPlayAction extends AbstractGameAction {
    private boolean highest,lowest;

    public DiscardAndPlayAction(boolean high,boolean low) {
        this.duration = Settings.ACTION_DUR_FAST;
        this.actionType = ActionType.WAIT;
        this.source = AbstractDungeon.player;
        this.highest = high;
        this.lowest = low;
    }

    ArrayList<AbstractCard> cloud = CloudPatch.cloudGroup.group;;
    AbstractCard high = null;
    AbstractCard low = null;

    public void update() {
        if(duration==Settings.ACTION_DUR_FAST){
            if(cloud.size()==0){
                this.isDone = true;
                return;
            }
            for (AbstractCard card : cloud) {
                if (high == null) {
                    high = card;
                } else if (card.costForTurn > high.costForTurn) {
                    high = card;
                }

                if (low == null) {
                    low = card;
                } else if (card.costForTurn>=0&&card.costForTurn < low.costForTurn) {
                    low = card;
                }
            }
        }
        else {
            if(cloud.size()>0){
                AbstractCard c = cloud.get(0);
                CloudPatch.resetBeforeRemove(c);
                CloudPatch.cloudGroup.moveToDiscardPile(c);
            }
            else {
                if(lowest&&low!=null){
                    AbstractCard card = low.makeStatEquivalentCopy();
                    card.purgeOnUse = true;
                    AbstractDungeon.player.limbo.group.add(card);
                    card.current_y = (float) Settings.HEIGHT / 2.0F - 300.0F * Settings.scale;
                    card.target_x = (float) Settings.WIDTH / 2.0F + 200.0F * Settings.xScale;
                    card.target_y = (float) Settings.HEIGHT / 2.0F;
                    card.targetAngle = 0.0F;
                    card.lighten(false);
                    card.drawScale = 0.12F;
                    card.targetDrawScale = 0.75F;
                    card.applyPowers();
                    this.addToTop(new NewQueueCardAction(card, true, false, true));
                    this.addToTop(new UnlimboAction(card));
                    if (!Settings.FAST_MODE) {
                        this.addToTop(new WaitAction(Settings.ACTION_DUR_MED));
                    } else {
                        this.addToTop(new WaitAction(Settings.ACTION_DUR_FASTER));
                    }
                    lowest = false;
                }
                if(highest&&high!=null){
                    AbstractCard card = high.makeStatEquivalentCopy();
                    card.purgeOnUse = true;
                    AbstractDungeon.player.limbo.group.add(card);
                    card.current_y = (float) Settings.HEIGHT / 2.0F - 300.0F * Settings.scale;
                    card.target_x = (float) Settings.WIDTH / 2.0F + 200.0F * Settings.xScale;
                    card.target_y = (float) Settings.HEIGHT / 2.0F;
                    card.targetAngle = 0.0F;
                    card.lighten(false);
                    card.drawScale = 0.12F;
                    card.targetDrawScale = 0.75F;
                    card.applyPowers();
                    this.addToTop(new NewQueueCardAction(card, true, false, true));
                    this.addToTop(new UnlimboAction(card));
                    if (!Settings.FAST_MODE) {
                        this.addToTop(new WaitAction(Settings.ACTION_DUR_MED));
                    } else {
                        this.addToTop(new WaitAction(Settings.ACTION_DUR_FASTER));
                    }
                    highest = false;
                }
            }
        }

        this.tickDuration();
    }
}


